package com.test.springai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 9/4/25 4:31 PM
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
@RequestMapping("/entity")
public class FormatResponseController {
  private final ChatClient chatClient;

  public FormatResponseController(ChatClient.Builder builder) {
    this.chatClient = builder.build();
  }

  @GetMapping("/chat")
  public Fruit entity(@RequestParam(value = "userInput") String userInput) {
    return this.chatClient.prompt().user(userInput).call().entity(Fruit.class);
  }

  @GetMapping("/list")
  public List<Fruit> entityList(@RequestParam(value = "userInput") String userInput) {
    return this.chatClient
        .prompt()
        .user(userInput)
        .call()
        .entity(new ParameterizedTypeReference<>() {});
  }

  @GetMapping("/list/string")
  public List<String> list(@RequestParam(value = "userInput") String userInput) {
    return this.chatClient
        .prompt()
        .user(userInput)
        .call()
        .entity(new ListOutputConverter(new DefaultConversionService()));
  }

  public record Fruit(String name, String month) {}
}
